using System;
using System.Collections.Generic;
using System.Text;
using G3.State;

namespace G3.AI
{
    class EvolAgent : IComparable<EvolAgent>
    {
        public double fitness; //the less the better

        private double[] wheelVector;
        private double[] throttleVector;
        private int n; //vector size

        public EvolAgent(double[] v, int halfVectorSize)
        {
            n = halfVectorSize;
            wheelVector = new double[n];
            throttleVector = new double[n];
            for (int i = 0; i < n; ++i)
            {
                wheelVector[i] = v[i];
            }
            for (int i = 0; i < n; ++i)
            {
                throttleVector[i] = v[i + n];
            }
        }

        public double[] getVector()
        {
            double[] v = new double[2 * n];
            for (int i = 0; i < n; ++i)
            {
                v[i] = wheelVector[i];
            }
            for (int i = 0; i < n; ++i)
            {
                v[i + n] = throttleVector[i];
            }
            return v;
        }

        public Pair Think(double[] points, double speed)
        {
            double[] v = new double[n];
            double[] a = new double[n];
            v[0] = speed;
            a[0] = speed;
            for (int i = 1; i < n; ++i)
            {
                v[i] = points[i - 1];
                a[i] = Math.Abs(v[i]);
            }
            Pair p = new Pair(scalarProduct(v, wheelVector), 100.0 - scalarProduct(a, throttleVector));
            return p;
        }

        private double scalarProduct(double[] v1, double[] v2)
        {
            double product = 0.0;
            for (int i = 0; i < n; ++i)
            {
                product += v1[i] * v2[i];
            }
            return product;
        }

        public int CompareTo(EvolAgent ea)
        {
            return fitness.CompareTo(ea.fitness);
        }
    }
}
